﻿package com.teaphy.coroutinecrawler.build_coroutine

import kotlinx.coroutines.*
import kotlin.system.measureTimeMillis

// runBlocking与coroutineScope都会等待其协程体以及所有子协程结束。
// runBlocking会阻塞当前线程来等待
// runBlocking是常规函数
// coroutineScope会挂起，不会阻塞当前线程
fun main(): Unit = runBlocking {

    val timeExe = measureTimeMillis {
        val result1 = test1()
        val result2 = test2()

        println("result1 : $result1")
        println("result2 : $result2")

        println("coroutineScope: $this")
    }
    println("timeExe: $timeExe")
}

suspend fun test1(): String {
    return withContext(Dispatchers.IO) {
        println("test1 start --> ")
        delay(2000)
        println(" --> test1 end")
        "test1"
    }
}

suspend fun test2(): String {
    return withContext(Dispatchers.IO) {
        println("test2 start --> ")
        delay(1000)
        println(" --> test2 end")
        "test2"
    }
}
